package com.yycwxt.modules.admin.shop;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.yycwxt.common.BaseService;
import com.yycwxt.domain.Flowfee;
import com.yycwxt.domain.Shop;

import com.yycwxt.domain.Subject;
import lombok.SneakyThrows;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
@Service
@Transactional
public class ShopService extends BaseService<ShopMapper, Shop> {


//    private ShopMapper shopMapper;
//
//    public ShopService(@Autowired ShopMapper shopMapper) {
//        this.shopMapper = shopMapper;
//    }
//    public List<Shop> selectByName(String name){
//        return shopMapper.selectByName(name);
//    }

//    public List<Shop>selectAll(){
//        return shopMapper.selectAll();
//    }

//    public int addShop(Shop shop){
//        return shopMapper.insert(shop);
//    }

//    public int deleteShop(Shop shop){
//        return shopMapper.deleteById(shop);
//    }

//    public int updateShop(Shop shop){
//        return shopMapper.updateById(shop);
//    }

    private ShopMapper shopMapper;
    @Autowired
    public ShopService(ShopMapper shopMapper) {
        this.shopMapper = shopMapper;
    }

    @SneakyThrows
    public boolean importFileToDB(MultipartFile file) {
//        if (deleteHistory) {
//            this.remove(null);
//        }

        EasyExcel.read(file.getInputStream(), Shop.class, new ReadListener<Shop>() {

//            private static final int BATCH_SIZE = 1000;
//            private List<Shop> ls = Lists.newArrayList();
//            private List<Shop> all = shopMapper.selectAll();
            @Override
            public void invoke(Shop data, AnalysisContext context) {
               ShopService.this.saveOrUpdate(data);
//                ls.add(data);
//                if (ls.size() >= BATCH_SIZE) {
//                    ShopService.this.saveBatch(ls);
//                    ls.clear();
//                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
//                ShopService.this.saveBatch(ls);
            }
        }).sheet().doRead();
        return true;
    }

    public List<Shop> selectByInput(String input){
        return shopMapper.selectByInput(input);
    }
}
